package com.milo.atomic;

import java.util.concurrent.atomic.AtomicInteger;


/***
 * 演示AtomicInteger的用法[原子更新基本类型类]
 * @author Milo Lee
 * @date 2022-03-14 15:12
 */
public class AtomicIntegerTest {

    static AtomicInteger ai = new AtomicInteger(1);

    public static void main(String[] args) {
        //取值然后设置一个新值
        int andSet = ai.getAndSet(2);
        System.out.println(andSet);
        //自增1
        int andIncrement = ai.getAndIncrement();
        System.out.println(andIncrement);
        //取值然后加5
        int andAdd = ai.getAndAdd(5);
        System.out.println(andAdd);
        //CAS交换
        boolean b = ai.compareAndSet(8, 100);
        //获取值
        int i = ai.get();
        System.out.println(i);
    }
}
